package com.aqie.medium.backtrack;

import java.util.ArrayList;
import java.util.List;

/**
 * todo
 * 526 优美的排列， 1-N 整数
 * 第 i 位的数字能被 i 整除
 * i 能被第 i 位上的数字整除
 */
public class CountArrangement {
    /**
     * 全排列 剪枝
     */
    int count = 0;
    public int countArrangement(int N) {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i <= N; i++){
            list.add(i);
        }
        backtrace(list, 1, N);
        return count;
    }

    private void backtrace(List<Integer> list, int i, int n) {
        if ( i > n){
          count++;
          return;
        }
        for (int j = i; j < list.size(); j++){
           int tmp = list.get(i);
           list.set(i,list.get(j));
           list.set(j, tmp);

           if (list.get(i) %i == 0 || i % list.get(i) == 0){
               backtrace(list,i+1, n);
           }
            tmp = list.get(i);
            list.set(i,list.get(j));
            list.set(j, tmp);
        }

    }
}
